'''
import pyaudio
from aip import AipSpeech

# 请自行填入你在百度智能云中申请的app_id, api_key, secret_key
APP_ID = '31958523'
API_KEY = 'RS2GM39mUASP9BD5kaMMzyW8'
SECRET_KEY = 'nD1T98lzG2RVxgPxwBaUGTDIXjF0voGS'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 录音并进行语音识别
def recognize_speech():
    # 配置录音参数
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 16000
    RECORD_SECONDS = 1

    # 创建PyAudio对象
    p = pyaudio.PyAudio()

    # 打开音频流
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    # 读取音频数据
    frames = []
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)

    # 停止音频流和PyAudio对象
    stream.stop_stream()
    stream.close()
    p.terminate()

    # 将音频数据转换为字节流
    audio_data = b''.join(frames)

    # 调用百度语音识别API进行识别
    result = client.asr(audio_data, 'pcm', RATE, {
        'dev_pid': 1537,  # 1537为普通话(支持简单的英文识别)
    })

    # 输出识别结果
    if result['err_no'] == 0:
        return result['result'][0]
    else:
        print(result['err_msg'])
        return None

# 不停地监听麦克风并进行语音识别
while True:
    print('请说话...')
    text = recognize_speech()
    if text:
        print('识别结果：', text)'''''
'''
import pyaudio
from aip import AipSpeech

# 请自行填入你在百度智能云中申请的app_id, api_key, secret_key
APP_ID = '31958523'
API_KEY = 'RS2GM39mUASP9BD5kaMMzyW8'
SECRET_KEY = 'nD1T98lzG2RVxgPxwBaUGTDIXjF0voGS'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 录音并进行语音识别
def recognize_speech():
    # 配置录音参数
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 16000
    RECORD_SECONDS = 1.5  # 增加录音时长为5秒

    # 创建PyAudio对象
    p = pyaudio.PyAudio()

    # 打开音频流
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    # 读取音频数据
    frames = []
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)

    # 停止音频流和PyAudio对象
    stream.stop_stream()
    stream.close()
    p.terminate()

    # 将音频数据转换为字节流
    audio_data = b''.join(frames)

    # 调用百度语音识别API进行识别
    result = client.asr(audio_data, 'pcm', RATE, {
        'dev_pid': 1537,  # 1537为普通话(支持简单的英文识别)
    })

    # 输出识别结果
    if result['err_no'] == 0:
        return result['result'][0]
    else:
        print(result['err_msg'])
        return None

# 不停地监听麦克风并进行语音识别
while True:
    print('请说话...')
    text = recognize_speech()
    if text:
        print('识别结果：', text)'''
       # break  # 识别到语音后退出循环




import pyaudio
import wave
from aip import AipSpeech

# Please replace with your own Baidu API credentials
APP_ID = '31958523'
API_KEY = 'RS2GM39mUASP9BD5kaMMzyW8'
SECRET_KEY = 'nD1T98lzG2RVxgPxwBaUGTDIXjF0voGS'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# Record audio and perform speech recognition
def recognize_speech():
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 16000
    RECORD_SECONDS = 2

    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    frames = []

    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)

    stream.stop_stream()
    stream.close()
    p.terminate()

    audio_data = b''.join(frames)

    result = client.asr(audio_data, 'pcm', RATE, {
        'dev_pid': 1537,
    })

    if result['err_no'] == 0:
        return result['result'][0]
    else:
        print(result['err_msg'])
        return None

# Main loop to continuously listen to microphone and perform speech recognition
while True:
    print('Please speak...')
    text = recognize_speech()
    if text:
        print('Recognition result:', text)
